from collections import OrderedDict

from django.shortcuts import render

# Create your views here.
from django.views import View

from contents.models import ContentCategory
from goods.models import GoodsChannel


class IndexView(View):
    def get(self, request):
        # 1. 准备商品分类对应的字典
        categories = OrderedDict()
        # 查询并展示商品分类 37个一级类别
        channels = GoodsChannel.objects.order_by('group_id', 'sequence')
        # 遍历所有频道
        for channel in channels:
            group_id = channel.group_id  # 当前组
            # 获取当前频道所在的组：只有11个组
            if group_id not in categories:
                categories[group_id] = {'channels': [], 'sub_cats': []}
            cat1 = channel.category  # 当前频道的类别
            # 追加当前频道
            categories[group_id]['channels'].append({
                'id': cat1.id,
                'name': cat1.name,
                'url': channel.url
            })
            # 查询二级和三级类别
            for cat2 in cat1.subs.all():  # 从一级类别查找二级类别
                cat2.sub_cats = []  # 给二级类别添加一个保存三级类别的列表 ?????????????
                for cat3 in cat2.subs.all():  # 从二级类别查找三级类别
                    cat2.sub_cats.append(cat3)  # 将三级类别添加到二级sub_cats
                # 将二级类别添加到一级类别的sub_cats
                categories[group_id]['sub_cats'].append(cat2)
        # 查询所有的广告类别
        contents = OrderedDict()
        content_categories = ContentCategory.objects.all()
        # 查询出未下架的广告并排序
        for content_category in content_categories:
            contents[content_category.key] = content_category.content_set.filter(status=True).order_by('sequence')
        # 渲染模板的上下文
        context = {  # 构造上下文
            'categories': categories,  # 分类数据
            'contents': contents,  # 广告数据
        }
        return render(request, 'index.html', context)
